1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 import java.util.*;
30 import static java.util.ResourceBundle.Control.*;
31
32 public class ControlFactoryTest {
33
34
35
36
37
38 private static interface Factory {
39 public ResourceBundle.Control getControl(List<String> formats);
40 public String name();
41 }
42
43 static int errors;
44
45 public static void main(String[] args) {
46
47 testControlFactory(new Factory() {
48 public ResourceBundle.Control getControl(List<String> formats) {
49 return ResourceBundle.Control.getControl(formats);
50 }
51 public String name() { return "getControl"; }
52 }, Locale.getDefault());
53
54
55 testControlFactory(new Factory() {
56 public ResourceBundle.Control getControl(List<String> formats) {
57 return ResourceBundle.Control.getNoFallbackControl(formats);
58 }
59 public String name() { return "getNoFallbackControl"; }
60 }, null);
61
62 if (errors > 0) {
63 throw new RuntimeException("FAILED: " + errors + " error(s)");
64 }
65 }
66
67 private static void testControlFactory(Factory factory, Locale loc) {
68 testGetControl(factory, loc, FORMAT_DEFAULT, "java.class", "java.properties");
69 testGetControl(factory, loc, FORMAT_CLASS, "java.class");
70 testGetControl(factory, loc, FORMAT_PROPERTIES, "java.properties");
71
72
73 String[][] data = {
74 { "java.class", "java.properties", "java.xml" },
75 { "java.class", "java.props" },
76 { "java.properties", "java.class" },
77 { "java.foo", "java.properties" },
78 { "java.foo" },
79 { null },
80 };
81 for (String[] fmts : data) {
82 try {
83 List<String> fmt = Arrays.asList(fmts);
84 ResourceBundle.Control control = factory.getControl(fmt);
85 error("getControl: %s%n", fmt);
86 } catch (IllegalArgumentException e) {
87 }
88 }
89
90
91 try {
92 ResourceBundle.Control control = factory.getControl(null);
93 error("%s: doesn't throw NPE.%n", factory.name());
94 } catch (NullPointerException npe) {
95 }
96 }
97
98 private static void testGetControl(Factory factory,
99 Locale loc,
100 final List<String> FORMATS,
101 String... fmtStrings) {
102 final ResourceBundle.Control CONTROL = factory.getControl(FORMATS);
103 List<String> fmt = CONTROL.getFormats("any");
104 if (fmt != FORMATS) {
105 error("%s: returns %s, expected %s.%n",
106 factory.name(), fmt, FORMATS);
107 }
108 ResourceBundle.Control control = null;
109
110
111 for (int i = 0; i < 10; i++) {
112 fmt = Arrays.asList(fmtStrings);
113 control = factory.getControl(fmt);
114 if (control != CONTROL) {
115 error("%s: doesn't return the singleton: got %s, expected %s%n",
116 factory.name(), control, CONTROL);
117 }
118 }
119
120
121 Locale defaultLocale = Locale.getDefault();
122 Locale nonDefaultLocale = defaultLocale.equals(Locale.US) ? Locale.JAPAN : Locale.US;
123 if (loc != null) {
124
125 Locale l = CONTROL.getFallbackLocale("any", nonDefaultLocale);
126 if (!defaultLocale.equals(l)) {
127 error("%s: getFallbackLocale doesn't return default locale. got %s, expected %s%n",
128 factory.name(), toString(l), toString(defaultLocale));
129 }
130 l = CONTROL.getFallbackLocale("any", defaultLocale);
131 if (l != null) {
132 error("%s: getFallbackLocale doesn't return null. got %s%n",
133 factory.name(), toString(l));
134 }
135 } else {
136
137 Locale l = CONTROL.getFallbackLocale("any", nonDefaultLocale);
138 if (l != null) {
139 error("%s: getFallbackLocale doesn't return null. got %s%n",
140 factory.name(), toString(l));
141 }
142 l = CONTROL.getFallbackLocale("any", defaultLocale);
143 if (l != null) {
144 error("%s: getFallbackLocale doesn't return null. got %s%n",
145 factory.name(), toString(l));
146 }
147 }
148 }
149
150 private static String toString(Locale loc) {
151 if (loc == null)
152 return "null";
153 return "\"" + loc.getLanguage() + "_" + loc.getCountry() + "_" + loc.getVariant() + "\"";
154 }
155
156 private static void error(String msg) {
157 System.out.println(msg);
158 errors++;
159 }
160
161 private static void error(String fmt, Object... args) {
162 System.out.printf(fmt, args);
163 errors++;
164 }
165 }